package com.example.demo.controller;

import com.example.demo.constant.enums.ResultEnum;
import com.example.demo.form.User.AddUserForm;
import com.example.demo.form.User.ListUserForm;
import com.example.demo.model.vo.ResultVo;
import com.example.demo.model.vo.UserVo;
import com.example.demo.service.IUserService;
import com.example.demo.utils.ResultVoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: LKP
 * @create: 2020/11/25
 * @Description:
 * @FileName: UserController
 * @History:
 * @自定义内容：
 */
@RestController
@Api(tags = "用户相关API")
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    /**
     * 根据用户名获取用户信息
     *
     * @param name 用户名
     * @return 用户信息
     */
    @ApiOperation("根据用户名获取用户信息")
    @GetMapping("/getUserByName/{name}")
    @ApiResponses(
            @ApiResponse(code = 200, message = "操作成功", response = ResultVo.class)
    )
    public ResultVo getUserByName(@PathVariable("name") String name) {
        return ResultVoUtil.success(userService.getUserByName(name));
    }

    /**
     * 添加用户
     *
     * @param userForm 用户表单数据
     * @return 成功或者失败
     */
    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public ResultVo addUser(@Validated @RequestBody AddUserForm userForm) {
        if (userService.addUser(userForm)) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(ResultEnum.ADD_ERROR);
    }

    /**
     * 获取用户列表
     *
     * @param listUserForm 表单数据
     * @return 用户列表
     */
    @ApiOperation("获取用户列表")
    @GetMapping("/listUser")
    @ApiResponses(
            @ApiResponse(code = 200, message = "操作成功", response = UserVo.class)
    )
    public ResultVo listUser(@Validated ListUserForm listUserForm) {
        return ResultVoUtil.success(userService.listUser(listUserForm));
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return 成功或者失败
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser/{id}")
    public ResultVo deleteUser(@PathVariable("id") int id) {
        if (userService.deleteUser(id)) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(ResultEnum.DELETE_ERROR);
    }

}
